/**
 * 
 */
package com.netjava.action;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.netjava.pojo.AucRecord;
import com.netjava.pojo.Auctioning;
import com.netjava.pojo.EndAuction;
import com.netjava.pojo.Product;
import com.netjava.pojo.User;
import com.netjava.service.IAucRecordService;
import com.netjava.service.IAuctioningService;
import com.netjava.service.IEndAuctionService;
import com.netjava.service.IProductService;
import com.netjava.service.IUserService;
import com.netjava.utils.FormatTime;

/**
 * @author chenchaohui
 *
 */
public class AuctionAction extends BaseAction {

	/**
	 * 用户参与竞拍
	 */
	public ActionForward auction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
		
		//登录的用户对象
		User user=(User)request.getSession().getAttribute("loginUser");
		IUserService userService=(IUserService)getBean("userService");
		//用户拥有的金点数
		Integer value = user.getCard().getCardValue();
		
		AucRecord aucRecord=new AucRecord();
			
		int productId=Integer.parseInt(request.getParameter("productId"));
			
			//通过产品id找到相应的产品
		IProductService productService=(IProductService)getBean("productService");
		Product product = productService.getProById(productId);
			
			//用户每拍一次就要改变商品的价格，即在原来的基础上加0.15，同时用户的金点值会加1(更新数据库)
			String price=new DecimalFormat("###,###,###.##").format(product.getCurrentPrice()+0.15f);   
			product.setCurrentPrice(Float.parseFloat(price));
			productService.attachDirty(product);
			user.getCard().setCardValue(value-1);
			userService.attachDirty(user);
			
			aucRecord.setUser(user);
			aucRecord.setProduct(product);
			aucRecord.setAucPrice(product.getCurrentPrice());
			FormatTime formatTime=new FormatTime();
			aucRecord.setAucTime(formatTime.timeFormat());
			
			//更新当前商品拍卖中的状态属性
			IAuctioningService auctioningService=(IAuctioningService)getBean("auctioningService");
			Auctioning auctioningProduct = auctioningService.getAuctioningById(productId);
			
			
			String remainTime = auctioningProduct.getRemainTime();
			if(null!=remainTime){
				//分别得到小时，分钟，秒
				int time1 = Integer.parseInt(remainTime.substring(0, 2));
				int time2 = Integer.parseInt(remainTime.substring(3, 5));
				int time3 = Integer.parseInt(remainTime.substring(6, 8));
				//如果该商品剩余时间少于15秒，用户每拍一次，时间都会回退到15秒，这时候需要更新数据库中auctioning表
				if(time1==0&&time2==0&&time3<15){
					auctioningProduct.setRemainTime("00:00:15");
					auctioningService.attachDirty(auctioningProduct);
				}
			}
			
			
			
			//保存拍卖记录到数据库
			IAucRecordService aucRecordService=(IAucRecordService)getBean("aucRecordService");
			aucRecordService.add(aucRecord);
			List<AucRecord> aucRecords = aucRecordService.getByProId(product);
			
			//只取10条记录在页面显示
			List<AucRecord> records=new ArrayList();
			if(aucRecords.size()<10){
			for(int i=0;i<aucRecords.size();i++){
				records.add(aucRecords.get(i));
			}
			}
			else if(aucRecords.size()>=10){
				for(int i=0;i<10;i++){
					records.add(aucRecords.get(i));
				}
			}
				
		request.setAttribute("product", product);
		request.setAttribute("price", price);
		request.setAttribute("auctioningProduct", auctioningProduct);
		request.setAttribute("aucRecords", records);
		ActionForward forward=mapping.getInputForward();
		forward=mapping.findForward("auctioning");//注册成功时返回的页面
		return forward;
	}
	
	
	/**
	 * 商品竞拍结束后的记录，找到用户竞拍得到的商品
	 */
	public ActionForward findEndAcuByUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
		
		IEndAuctionService endAuctionService=(IEndAuctionService)getBean("endAuctionService");
		//登录的用户对象
		User user=(User)request.getSession().getAttribute("loginUser");
		List<EndAuction> endAuctions = endAuctionService.findByUserId(user);
		List<EndAuction> endauctions=new ArrayList<EndAuction>();
		
		ActionForward forward=mapping.getInputForward();
		
		if(endAuctions.size()<=0){
			request.setAttribute("msg", "你没有竞拍到任何商品");
			
			forward=mapping.findForward("showEndAcu");
		}
		else{
			for(int i=0;i<endAuctions.size();i++){
				EndAuction endAuction=new EndAuction();
				endAuction=endAuctions.get(i);
				String time=endAuction.getPayTime();
				
				
				//判断商品是否付款
				if(null!=time){
					//付款的时间不为空说明商品已经付款
					endauctions.add(endAuction);
					forward=mapping.findForward("showPayPro");
				}
				//判断商品是否付款
				else{
					//说明商品还没有付款
					endauctions.add(endAuction);
					forward=mapping.findForward("showEndAcu");
				}
			}
			
			if(endauctions.size()<=0){
				request.setAttribute("msg", "还没有记录！");
			}
			request.setAttribute("endauctions", endauctions);
		}
		
		
		return forward;
		
	}
	/**
	 * 用户管理，查询该用户所有的竞拍中商品
	 * 
	 */
 public ActionForward getAutionByUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
	 
		int page=1;
		if(null!=request.getParameter("page")){
			 page=Integer.parseInt(request.getParameter("page"));
		}
		Integer num=10;
		//设置末页
		Integer lastPage;
		
		
	 
	//得到用户对象
	 User user=(User)request.getSession().getAttribute("loginUser");
	 IAucRecordService aucRecordService=(IAucRecordService)getBean("aucRecordService");
	
	 List<AucRecord> records=aucRecordService.getNumAcu(user, page, num);
	 List<AucRecord> aucRecords=new ArrayList<AucRecord>();	 
	 
	 //总记录数目
	 List<AucRecord> totalaucRecords=aucRecordService.getByUserId(user);
	
	 
	 
	 for(int i=0;i<records.size();i++){
		 AucRecord aucRecord=records.get(i);
		 Product pro = aucRecord.getProduct();
		 //判断商品是否正在竞拍中，是否已经在队列中存在
		 if(pro.getState()==1){
			 aucRecords.add(aucRecord);	
		 }
		
	     }
	  if(aucRecords.size()<=0){
			 request.setAttribute("msg", "没有记录！");
			 request.setAttribute("flag",0);
		 }
	  else{
		  request.setAttribute("flag",1);
	  }
	  
	  if(totalaucRecords.size() % num != 0) { 
		  lastPage = totalaucRecords.size() / num + 1; 
	     } else { 
	    	 lastPage = totalaucRecords.size() / num; 
	     } 
	  request.setAttribute("lastPage",lastPage);
	
	 //设置当前页
	 request.setAttribute("currentPage",page);
	 request.setAttribute("aucRecords", aucRecords);
	 ActionForward forward=mapping.getInputForward();
	 forward=mapping.findForward("allAution");
	 return forward;

 }
	

}
